package com.jams.goodox.widget;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.DialogFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.NumberPicker;

import com.jams.goodox.R;

import java.lang.reflect.Field;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 数值日期选择器
 * Created by jams_zhen on 2016/7/9.
 */
public class NumberPickerDialog extends DialogFragment implements NumberPicker.OnValueChangeListener {

    @Bind(R.id.number_picker)
    CustomNumberPicker numberPicker;

    private String[] lists;
    private OnNumberPickerClick onNumberPickerClick;
    private int chooseDate;
    private int startPosition;

    public static NumberPickerDialog getNumberPickerDialog(int position,String[] lists){
        NumberPickerDialog numberPickerDialog=new NumberPickerDialog();
        Bundle bundle=new Bundle();
        bundle.putInt("posi",position);
        bundle.putStringArray("list",lists);
        numberPickerDialog.setArguments(bundle);
        return numberPickerDialog;
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        if(context instanceof OnNumberPickerClick){
            onNumberPickerClick=(OnNumberPickerClick)context;
        }
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        startPosition = getArguments().getInt("posi");
        lists = getArguments().getStringArray("list");
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        final Window window = getDialog().getWindow();
        View view = inflater.inflate(R.layout.dialog_number_picker, container);
        ButterKnife.bind(this, view);

        numberPicker.setDisplayedValues(lists);
        numberPicker.setMinValue(0);
        numberPicker.setMaxValue(lists.length - 1);
        numberPicker.setValue(startPosition>=0?startPosition:0);
        numberPicker.setDescendantFocusability(NumberPicker.FOCUS_BLOCK_DESCENDANTS);
        setNumberPickerDividerColor(numberPicker);
        numberPicker.setOnValueChangedListener(this);

        window.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));//注意此处
        window.setLayout(-1, -2);//这2行,和上面的一样,注意顺序就行;
        return view;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }

    @OnClick({R.id.cancel, R.id.ok})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.cancel:
                getDialog().cancel();
                break;
            case R.id.ok:
                if (onNumberPickerClick != null)
                    onNumberPickerClick.onClick(chooseDate);
                break;
        }
    }

    /**
     * 设置数字选择器分割线高度&颜色
     *
     * @param numberPicker
     */
    private void setNumberPickerDividerColor(NumberPicker numberPicker) {
        NumberPicker picker = numberPicker;
        Field[] pickerFields = NumberPicker.class.getDeclaredFields();
        for (Field pf : pickerFields) {
            if (pf.getName().equals("mSelectionDivider")) {
                pf.setAccessible(true);
                try {
                    //设置分割线的颜色值
                    pf.set(picker, new ColorDrawable(this.getResources().getColor(R.color.divider_line)));
                } catch (IllegalArgumentException e) {
                    e.printStackTrace();
                } catch (Resources.NotFoundException e) {
                    e.printStackTrace();
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                }
                break;
            }
        }
        // 分割线高度
        for (Field pf : pickerFields) {
            if (pf.getName().equals("mSelectionDividerHeight")) {
                pf.setAccessible(true);
                try {
                    int result = 1;
                    pf.set(picker, result);
                } catch (Exception e) {
                    e.printStackTrace();
                }
                break;
            }
        }
    }

    /**
     * 数字选择器变化监听器
     *
     * @param picker
     * @param oldVal
     * @param newVal
     */
    @Override
    public void onValueChange(NumberPicker picker, int oldVal, int newVal) {
        chooseDate = newVal;
    }

    /**
     * 确定按钮回调接口
     */
    public interface OnNumberPickerClick {
        void onClick(int position);
    }
}
